package com.school.movie.business.web.controller;

import com.school.movie.business.web.model.vo.*;
import com.school.movie.common.utils.BaseConstants;
import com.school.movie.common.utils.ContextUtils;
import com.school.movie.common.bean.PageBean;
import com.school.movie.service.imp.MovieHallServiceImp;
import com.school.movie.service.imp.SysLogServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/movieHall")
public class MovieHallController {

    @Autowired
    MovieHallServiceImp movieHallService;
    @Autowired
    SysLogServiceImp logService;

    @RequestMapping("/list")
    public String list(PageBean pager, MovieHallVo movieHall, HttpSession session, ModelMap map) {

        if (movieHall.getAgentName() == null) {
            if (session.getAttribute("movieHall") != null) {
                movieHall = (MovieHallVo) session.getAttribute("movieHall");
            }
        } else {
            session.setAttribute("movieHall", movieHall);
        }
        Manager mgr = ContextUtils.getManagerInfo();
        if (mgr.getManagerType() != 3) {
            movieHall.setAgentId(mgr.getAgentId());
        }

        List<AgentInfo> agentList = movieHallService.agentList(movieHall.getAgentId());
        int cnt = movieHallService.count(movieHall);
        pager.setTotalRows(cnt);
        List<MovieHallVo> movieHallList = movieHallService.list(movieHall, pager);
        pager.setData(movieHallList);
        map.addAttribute("pager", pager);
        map.addAttribute("movieHall", movieHall);
        map.addAttribute("agentList", agentList);
        return "movieHall";
    }

    @RequestMapping("/del")
    public String del(String ids, PageBean pager, ModelMap map, HttpSession session) {

        MovieHallVo movieHall = new MovieHallVo();
        if (session.getAttribute("movieHall") != null) {
            movieHall = (MovieHallVo) session.getAttribute("movieHall");
        }
        System.out.println("ids=" + ids);
        String arr[] = ids.split(",");
        movieHallService.del(arr);
        logService.add(BaseConstants.LOG_DEL, "删除了编号为：" + ids + "的影厅");
//        int cnt = movieHallService.count(movieHall);
//        pager.setTotalRows(cnt);
//        List<MovieHallVo> movieHallList = movieHallService.list(movieHall,pager);
//        pager.setDatas(movieHallList);
//        map.addAttribute("pager",pager);
        return list(new PageBean(), movieHall, session, map);
    }

    @RequestMapping("/update")
    public String update(int hallId, ModelMap map, HttpSession session) {
        Manager mgr = ContextUtils.getManagerInfo();
        List<AgentInfo> agentList = null;
        if (mgr.getManagerType() != 3) {
            agentList = movieHallService.agentList(mgr.getAgentId());
        } else {
            agentList = movieHallService.agentList(0);
        }
        List<HallTypeVo> hallTypeList = movieHallService.hallTypeList();
        MovieHallVo movieHall = movieHallService.getById(hallId);
        map.addAttribute("movieHall", movieHall);
        map.addAttribute("agentList", agentList);
        map.addAttribute("hallTypeList", hallTypeList);
        return "movieHallAdd";
    }

    @RequestMapping("/init")
    public String init(ModelMap map) {
        Manager mgr = ContextUtils.getManagerInfo();
        List<AgentInfo> agentList;
        if (mgr.getManagerType() != 3) {
            agentList = movieHallService.agentList(mgr.getAgentId());
        } else {
            agentList = movieHallService.agentList(0);
        }
        MovieHallVo hall = new MovieHallVo();
        hall.setState(1);
        List<HallTypeVo> hallTypeList = movieHallService.hallTypeList();
        map.addAttribute("movieHall", hall);
        map.addAttribute("agentList", agentList);
        map.addAttribute("hallTypeList", hallTypeList);
        return "movieHallAdd";
    }

    @RequestMapping("/save")
    public String save(MovieHallVo movieHall, ModelMap map, HttpSession session) {
        if (movieHall.getHallId() > 0) {
            movieHallService.update(movieHall);
            logService.add(BaseConstants.LOG_UPDATE, movieHall.toString());
        } else {
            movieHallService.add(movieHall);
            logService.add(BaseConstants.LOG_ADD, movieHall.toString());
        }
        //todo 更改了影厅行列的信息需要重新生成座位表


//        int cnt = movieHallService.count();
//        PageObject pager = new PageObject();
//        pager.setTotalRows(cnt);
//        List<MovieHallVo> movieHallList = movieHallService.list(pager);
//        pager.setDatas(movieHallList);
//        map.addAttribute("pager",pager);
        return list(new PageBean(), movieHall, session, map);
    }

    /**
     * 生成座位
     *
     * @param hall
     * @param map
     * @return
     */
    @RequestMapping("/seat")
    public String seat(MovieHallVo hall, ModelMap map) {
        hall = movieHallService.getById(hall.getHallId());
        if (hall.getSeatNum() == 0) {
            movieHallService.seatAdd(hall.getHallId());
        }
        List<HallSeatVo> seatList = movieHallService.getSeatByHallId(hall.getHallId());
        map.addAttribute("hall", hall);
        map.addAttribute("seatList", seatList);
        return "movieHallSeat";
    }

    /**
     * 重新生成座位
     *
     * @param hall
     * @param map
     * @return
     */
    @RequestMapping("/reSeat")
    public String reSeat(MovieHallVo hall, ModelMap map) {
        hall = movieHallService.getById(hall.getHallId());
        movieHallService.seatDel(hall.getHallId());
        movieHallService.seatAdd(hall.getHallId());
        List<HallSeatVo> seatList = movieHallService.getSeatByHallId(hall.getHallId());
        map.addAttribute("hall", hall);
        map.addAttribute("seatList", seatList);
        return "movieHallSeat";
    }

    /**
     * 设置座位状态
     */
    @RequestMapping("/state")
    @ResponseBody
    public Map<String, Integer> state(HallSeatVo seat) {
        movieHallService.seatUpdate(seat);
        Map<String, Integer> map = new HashMap(2);
        map.put("code", seat.getState());
        return map;
    }
}
